package org.winexpress

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
import org.codehaus.groovy.grails.web.json.JSONArray


class CountryController {
    // shared methods
    def sendResponse(statusCode) {
        if (statusCode == 404) {
            JSONObject o = new JSONObject()
            o.put(statusCode, true)
            render o as JSON
        }
        
        response.status = statusCode
        render response
        
        // 200-OK (e.g. GET success)
        // 201-Created (e.g. POST success)
        // 403-Forbidden (e.g. LOGIN failure)
        // 400-Bad Request (e.g. PUT,POST,DELETE failure)
        // 404-Not Found (e.g. GET failure)
        // 500-Internal Server Error (other...)
    }
    

    // REST operations
    // GET
    def show = {
        // println 'CountryController >> Show >> START'
        
        CountryManagement ctyman = new CountryManagement()
        
        if (params.id && Country.exists(params.id)) {    
            def cty = ctyman.getCountryByID(params.id)
            render cty as JSON
            sendResponse(200)
            
//            println 'Show country by ID >> ' + cty.id + " " + cty
        }
        else
        if (params.countryName) {    
            def cty = ctyman.getCountryByCountryName(params.countryName)
            render cty as JSON
            sendResponse(200)
            
//            println 'Show country by ID >> ' + cty.countryName + " " + cty.id
        }
        else {
            def allCTY = ctyman.listCountries()
            if (allCTY.length() > 0) {
                render allCTY as JSON
                sendResponse(200)
            }
            else {
                sendResponse(404)
            }  
            
//            println 'Listing all countries >> ' + allCTY
        }
        
        // println 'CountryController >> Show >> END'
    }
    
    // PUT
    def update = {
        // println 'CountryController >> Update >> START'
        
        JSONObject o = request.JSON
        
        CountryManagement ctyman = new CountryManagement()
        
        def cty = ctyman.getCountryByID(o.get("id"))
        if (o.has("countryName")) { cty.countryName = o.get("countryName"); println cty.countryName }
        
        def result = ctyman.updateCountry(cty)
        if (result == true) {
            // println 'CountryController >> Update >> ' + result
            sendResponse(200)
        }
        else {
            // println 'CountryController >> Update >> ' + result
            sendResponse(400)
        }
        

        // println 'CountryController >> Update >> END'

    }
    
    // DELETE
    def delete = {
        // println 'CountryController >> delete >> START'
        
        CountryManagement ctyman = new CountryManagement()
        
        if (params.id && Country.exists(params.id)) {    
            def result = ctyman.deleteCountry(id)
            
            if (result == true) {
                // println 'CountryController >> delete >> ' + result
                sendResponse(200)
            }
            else {
                // println 'CountryController >> delete >> ' + result
                sendResponse(400)
            }
        }
        
        // println 'CountryController >> delete >> END'
    }
    
    // POST
    def save = {
        // println 'CountryController >> Save >> START'
        
        JSONObject o = request.JSON
        
        CountryManagement ctyman = new CountryManagement()
        
        def result = ctyman.addCountry(o)
        if (result == true) {
            // println 'CountryController >> Save >> ' + result
            sendResponse(201)
        }
        else {
            // println 'CountryController >> Save >> ' + result
            sendResponse(400)
        }
        
        // println 'CountryController >> Save >> END'
    }
}
